﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SQLite;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MainFrame
{
    public partial class FormLogin : Form
    {

        
        public FormLogin()
        {
            InitializeComponent();
            loadTips.loadTipsInLogin = TipsDisplay;
        }
        public void TipsDisplay(string tips)
        {
            if (InvokeRequired)
            {
                this.Invoke(new Action(() =>
                {
                    lbl_loadTips.Text = tips;
                    lbl_loadTips.Refresh();//强制更新控件值，不然会等UI处理完才更新
                }));
            }
            else
            {
                lbl_loadTips.Text = tips;
                lbl_loadTips.Refresh();
            }

        }
        private void btn_createTable_Click(object sender, EventArgs e)
        {
            GlobalVar.dbOperate.CreateTable(GlobalVar.tableName, GlobalVar.tableCols, GlobalVar.tableTypes);
        }

        private void FormLogin_Load(object sender, EventArgs e)
        {
            cbx_userLevel.SelectedIndex = 0;
        }
        private string[] getTbxText()
        {
            try
            {
                string[] ret;
                string userName_s = tbx_userName.Text.Trim();
                string password_s = tbx_password.Text.Trim();
                string userLevel_s = cbx_userLevel.SelectedItem.ToString().Trim();
                if (string.IsNullOrEmpty(userName_s) || string.IsNullOrEmpty(password_s) || string.IsNullOrEmpty(userLevel_s))
                {
                    MessageBox.Show("用户名、用户密码、用户权限均不能为空");
                    return null;
                }
                else
                {
                    ret = new string[] { userName_s, password_s, userLevel_s };
                }
                return ret;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
                return null;
            }

        }
        private void btn_login_Click(object sender, EventArgs e)
        {
            try
            {
                btn_login.Enabled = false;
                SQLiteDataReader temp = null;
                string[] opera = new string[] { "=", "=", "=" };
                string[] colOpera = new string[] { "AND", "AND" };
                string[] readCon = getTbxText();
                GlobalVar.dbOperate.ReadTable(GlobalVar.tableName, null, GlobalVar.tableCols, opera, colOpera, readCon, ref temp);
                if (temp == null)
                {
                    MessageBox.Show("用户名和密码不正确，请重试");
                    btn_login.Enabled = true;
                    return;
                }
                if (temp.Read())
                {
                    //this.Close();
                    if (readCon[2] == "技术员")
                    {

                        MotionControl.GlobalVariable.OperLevel = (int)MotionControl.operLevel.technician;
                        if (GlobalVar.isChangeUser)
                        {
                            changeUser.changeUserID(true);
                        }
                    }
                    else if (readCon[2] == "操作员")
                    {
                        MotionControl.GlobalVariable.OperLevel = (int)MotionControl.operLevel.operatorStaff;
                        if (GlobalVar.isChangeUser)
                        {
                            changeUser.changeUserID(false);
                        }
                    }
                    else
                    {
                        MotionControl.GlobalVariable.OperLevel = (int)MotionControl.operLevel.admin;
                        if (GlobalVar.isChangeUser)
                        {
                            changeUser.changeUserID(true);
                        }
                    }
                    MotionControl.GlobalVariable.OperName = tbx_userName.Text.Trim();
                    if (GlobalVar.isChangeUser)
                    {
                        changeUser.changeUserMain();
                    }
                    
                    //lbl_loadTips.Text = "加载中，请稍候...";
                    loadTips.loadTipsInLogin("加载中，请稍候...");
                    btn_login.Text = "登陆中.";
                    btn_login.Refresh();
                    Thread.Sleep(50);
                    if (GlobalVar.frmMain == null || GlobalVar.frmMain.IsDisposed)
                    {
                        GlobalVar.frmMain = new frm_MainFrame();
                        GlobalVar.frmMain.TopLevel = true;
                        GlobalVar.frmMain.Show();
                    }
                    else
                    {
                        GlobalVar.frmMain.Activate();
                    }
                    Thread.Sleep(10);
                    this.Hide();
                }
                else
                {
                    MessageBox.Show("用户名和密码不正确，请重试");
                }
                //while (temp.Read())
                //{
                //    string UserName = temp.GetString(temp.GetOrdinal("UserName"));
                //    string UserPassword = temp.GetString(temp.GetOrdinal("Password"));
                //    string UserLevel = temp.GetString(temp.GetOrdinal("Level"));
                //    string[] lvContext = new string[] { UserName, UserPassword, UserLevel };

                //}
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            if (GlobalVar.isChangeUser)
            {
                this.Hide();
            }
            else
            {
                Application.Exit();
            }

        }
    }
}
